package com.springcloud.redis.service;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.config.Config;

import java.sql.Time;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author liyanhao
 * @Date 2022-11-29 16:00
 */
public class WatchDogDemo {
    public static final String LOCKKEY = "AAA";

    private static Config config;
    private static Redisson redisson;
    static {
        config = new Config();
        //看门狗的业务线程执行时候续约的时间
        config.setLockWatchdogTimeout(30000);
        config.useSingleServer().setAddress("redis://192.168.139.20:6379").setDatabase(0);
        redisson = (Redisson) Redisson.create(config);
    }

    public static void main(String[] args) {

        HashMap<Object, Object> map = new HashMap<>();


        RLock redissonLock = redisson.getLock(LOCKKEY);
        redissonLock.lock();
        try {
            System.out.println("11111111111--------------biz");
            //暂停几秒钟线程
            TimeUnit.SECONDS.sleep(5);

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (redissonLock.isLocked() && redissonLock.isHeldByCurrentThread()) {
                redissonLock.unlock();
            }
        }
        System.out.println("加锁结束---------");
        redisson.shutdown();
    }


}
